package com.yilin.tms.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.yilin.commons.encrypt.EncryptUtils;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.data.insurance.InsuranceOder;
import com.yilin.tms.core.entity.order.transit.TransitOrder;
import com.yilin.tms.core.entity.truck.TransportLicense;
import com.yilin.tms.data.mapper.InsuranceOrderMapper;
import com.yilin.tms.data.service.IInsuranceService;
import com.yilin.tms.core.remote.IOrderRemote;
import com.yilin.tms.core.remote.IVehicleRemote;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional
public class InsuranceService extends ServiceImpl<InsuranceOrderMapper, InsuranceOder> implements IInsuranceService {

    @Resource
    IVehicleRemote vehicleRemote;
    @Resource
    IOrderRemote orderRemote;

    @Resource
    InsuranceOrderMapper insuranceOrderMapper;

    /**
     * 始运输 生成保单
     */
    @Override
    public void generateInsuranceData(String transportNo ) {
        InsuranceOder insuranceOder = insuranceOrderMapper.selectOne(new QueryWrapper<InsuranceOder>().eq("transport_no", transportNo));
        if (insuranceOder!=null) return;
        TransitOrder localTransitOrder  = ParamUtil.isSuccess(orderRemote.getTransitOrderByNo(transportNo));
        insuranceOder = new InsuranceOder();
        insuranceOder.setTransportNo(localTransitOrder.getTransportNo());
        insuranceOder.setShipperName(localTransitOrder.getShipperOrganizeName());
        insuranceOder.setTruckUserName(localTransitOrder.getTransitTruckUserName());
        insuranceOder.setTruckTypeName(localTransitOrder.getTransitTruckTypeName());
        insuranceOder.setTruckLoadWeight(localTransitOrder.getTransitTruckLoadingWeight());
        if (localTransitOrder.getTransitTruckWeight() > 4.5) {
            TransportLicense transportLicense = ParamUtil.isSuccess(vehicleRemote.getTransportLicenseByVehicleId(localTransitOrder.getTransitTruckId()));
            insuranceOder.setTruckTransformNo(EncryptUtils.aesDecrypt(transportLicense.getTransLicenseNo(),null));
        }
        insuranceOder.setTruckNo(localTransitOrder.getTransitTruckNo());
        insuranceOder.setTrailerNo(localTransitOrder.getTransitTrailerNo());
        insuranceOder.setGoodsTypeName(localTransitOrder.getGoodsTypeName());
        insuranceOder.setInsuranceAmount(localTransitOrder.getInsuredAmount());
        insuranceOder.setInsuranceMoney(localTransitOrder.getInsuredCost());
        insuranceOder.setDeliveryTime(localTransitOrder.getDeliverTime());
        insuranceOder.setDeliveryAddress(localTransitOrder.getPlanningConsignorAddress());
        insuranceOder.setConsigneeTime(localTransitOrder.getReceiptTime());
        insuranceOder.setConsigneeAddress(localTransitOrder.getPlanningConsigneeAddress());
//        insuranceOder.setMapKilometer(localTransitOrder.getKilometer());
        insuranceOder.setTruckState("运输中");
        insuranceOder.setInsuranceState(InsuranceOder.InsuranceStatus.create);
        insuranceOrderMapper.insert(insuranceOder);
    }


}
